<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/01/26
 * Time: 下午 10:36
 */

namespace app\admin\model;

use think\Model;

/**
 * Class Sysmsgabout
 * @package app\admin\model
 * 车型-模型
 */
class Vehicle extends Model {
    /**
     * @param $pageNum
     * @param $numPerPage
     * @param $title
     * @return mixed
     * 车型-列表
     */
    public function getVehicleList($pageNum, $numPerPage, $title) {
        $map = [];
        if (!empty($title)) {
            $map['title'] = ['like', "%{$title}%"];
        }
        $list = db("vehicle")
                ->where($map)
                ->page($pageNum, $numPerPage)
                ->order('id asc')
                ->select();
        return $list;
    }

    /**
     * @param $title
     * @return mixed
     * 车型-数量
     */
    public function getVehicleCount($title) {
        $map = [];
        if (!empty($title)) {
            $map['title'] = ['like', "%{$title}%"];
        }
        $count = db("vehicle")
                ->field("id")
                ->where($map)
                ->count();
        return $count;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \Exception
     * 车型-添加
     */
    public function doAddVehicle($data) {
        if (db("vehicle")->where('title', $data['title'])->find()) {
            exception('该车型已存在');
        }
        $data['sm_title'] = mb_substr($data['title'], 0, 1, 'utf-8');
        $result = db("vehicle")->insert($data);
        if (!$result) {
            exception('添加失败');
        }
        return $result;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 车型-修改
     */
    public function doModifyVehicle($data) {
        $id = $data['id'];
        unset($data['id']);
        $data['sm_title'] = mb_substr($data['title'], 0, 1, 'utf-8');
        $result = db("vehicle")->where('id', $id)->update($data);
        if (!$result) {
            exception('修改失败');
        }
        return $result;
    }

    /**
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 车型-删除
     */
    public function delVehicle($id) {
        $del = db("vehicle")
                ->where("id", $id)
                ->delete();
        if (!$del) {
            exception('删除失败');
        }
        return $del;
    }

}
